﻿using Prism.Commands;
using Prism.Mvvm;
using System;

namespace CCManger.ModuleSocket.ViewModels
{
    public class MainSocketViewModel : BindableBase
    {
        /// <summary>
        /// 信息行数
        /// </summary>
        private int _ConsoleLogLine = 0;

        private string _ConsoleMsg;
        private DelegateCommand _StartCommand;
        private string _StartMsg = "未启动";

        private string _Title;

        public MainSocketViewModel()
        {
            Header = "Sock服务";
        }

        /// <summary>
        /// 显示的控制台信息
        /// </summary>
        public string ConsoleMsg
        {
            get { return _ConsoleMsg; }
            set { SetProperty(ref _ConsoleMsg, value); }
        }

        public string Header
        {
            get { return _Title; }
            set { SetProperty(ref _Title, value); }
        }

        public DelegateCommand StartCommand =>
                    _StartCommand ?? (_StartCommand = new DelegateCommand(ExecuteStartCommand));

        /// <summary>
        /// 启动相关提示提示信息
        /// </summary>
        public string StartMsg
        {
            get { return _StartMsg; }
            set { SetProperty(ref _StartMsg, value); }
        }

        private void ExecuteStartCommand()
        {
            ClientSocket.Run.Start(LogStartInfo, LogConsoleInfo);
        }

        private void LogConsoleInfo(string info)
        {
            _ConsoleLogLine++;
            if (_ConsoleLogLine > 400)
            {
                ConsoleMsg = ConsoleMsg.Substring(0, ConsoleMsg.Length - ConsoleMsg.LastIndexOf(Environment.NewLine));
                _ConsoleLogLine--;
            }
            string date = DateTime.Now.ToString();
            ConsoleMsg = date + info + Environment.NewLine + ConsoleMsg;
        }

        private void LogStartInfo(string info)
        {
            StartMsg = info;
        }
    }
}